#----------------------------------------------------------
#DBG_PREFIX is the prefix to files that are built for
# debugging purposes
#libraries are the libraries passed to pkg-config.
#dbg_app_flags are flags specific to the debug version
#app_flags are flags specific to the release version.
#----------------------------------------------------------
#Oyea, if you want the version number in your code, act
# like it's #DEFINE'd to 'APPNAME_VERSION' ( where APPNAME
# is the name of your program -- and this directory)
# APPNAME_DEBUG - true if command is "make debug", false otherwise
# Of course, both of these macros are in ALL_CAPS. :)
#----------------------------------------------------------
#Everything below is kinda crucial. Don't screw with it.
#----------------------------------------------------------

CPPSOURCES:=$(wildcard src/*.cpp)
CSOURCES:=$(wildcard src/*.c)
OBJECTS:=$(patsubst src/%.cpp,obj/%,$(CPPSOURCES)) $(patsubst src/%.c,obj/%,$(CSOURCES))
DBOBJECTS:=$(patsubst src/%.cpp,obj/$(DBG_PREFIX)%,$(CPPSOURCES)) $(patsubst src/%.c,obj/%,$(CSOURCES))

APPNAME:=$(shell python -c "import os;print(os.getcwd().split(os.sep)[-1])")
APPVERS:=$(shell python -c "f=open(\"version.txt\");ver=int(f.read())+1;print(ver);f.close()")
DBGAPPNAME:= $(DBG_PREFIX)$(APPNAME)

local_libraries:=$(shell python -c "import os;print(*[z for z in os.walk(os.path.join(os.getcwd(),'lib'))][0][1])")

lib_flags:=$(foreach lib,$(local_libraries),-I./lib/$(lib)/include )\
 $(foreach lib,$(libraries),$(shell pkg-config --cflags $(lib)))\
  -D "$(shell echo $(APPNAME)_VERSION| tr a-z A-Z)=$(APPVERS)" -I./include

lib_links:=$(foreach lib,$(local_libraries), "-l$(lib)")\
 $(foreach lib,$(libraries),$(shell pkg-config --libs $(lib)))\
 -static-libgcc -static-libstdc++ -L./lib

flags:= $(lib_flags) -D "$(shell echo $(APPNAME)_debug| tr a-z A-Z)=false"
links:= $(lib_links)

dbg_flags:=$(lib_flags) -D "$(shell echo $(APPNAME)_debug| tr a-z A-Z)=true" -g
dbg_links:=$(lib_links) -g

increment_version:="f=open(\"version.txt\",\"w\");f.write(\"{}\".format($(APPVERS)+1));f.close()"

.PHONY:clean run rebuild build_main build_debug debug all

all:build_main build_debug

clean:
	@rm $(OBJECTS) -f
	@rm $(DBOBJECTS) -f
	@rm $(DBGAPPNAME)* -f
	@rm $(APPNAME)* -f
	$(foreach lib,$(local_libraries), @echo Cleaning $(lib)...;cd lib/$(lib);make clean;cd ../..)

libs_debug:
	$(shell for lib in $(local_libraries);do cd "lib/${lib}";make build_debug;cd ../..;done)

libs:
	$(shell for lib in $(local_libraries);do cd "lib/${lib}";make build_main;cd ../..;done)

obj/%:src/%.c
	@echo Compiling C Object: $@
	@$(CC) $< -c -o $@ $(flags)

obj/%:src/%.cpp
	@echo Compiling C++ Object: $@
	@$(CXX) $< -c -o $@ $(flags)

obj/$(DBG_PREFIX)%:src/%.cpp
	@echo Compiling debug-C++ Object: $@
	@$(CXX) $< -c -o $@ $(dbg_flags)

obj/$(DBG_PREFIX)%:src/%.c
	@echo Compiling debug-C Object: $@
	@$(CC) $< -c -o $@ $(dbg_flags)

rebuild:clean build_main